
var wallet = require('../../wallet.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
      activeId:1,
      checkedId:false,
      inId:true,
      textId:true,
      textData:[],
      addressBtc:'',//btc
      sign:'',//签名
      addressEth:'',
      pswd:'',
      pswdAgin:'',
      wallet:{},
      popArr:[],
      InputVal:'',
      Hdwallet:{},
      restoreLang:{},
      placeHolder:0
  },
  inSum(e){
    //console.log(e.detail.value)
    var val = e.detail.value;
    if(/\s+/g.test(val)){
     console.log(e.detail)
      var $this = this
      val.replace(/\S+/g, function (a, b, c) {
        $this.data.textData.push(a)
      })
      this.setData({
        textData: this.data.textData,
        inId: this.data.textData.length >= 12 ? false : true,
        InputVal: '',
        placeHolder:1
      })
    }
    
  },
 /* inSum(e){
    //console.log(e)
    this.setData({
      InputVal:e.detail.value
    })

  },*/
  goback(){
    wx.navigateBack({
      delta: 1
    })
  },
  deleteEle(e){
    var eId = e.target.id;
    var dataArr = this.data.textData
    dataArr.splice(eId,1)
    //console.log(dataArr)
    this.setData({
      textData: dataArr,
      inId: this.data.textData.length <=12? true : false,
      placeHolder:0
    })
  },
  helpWord(textData){
    var mnemonic2 = textData.join(' ')
    var hdWallet = wallet.productWallet(mnemonic2) 
    this.setData({
      Hdwallet : hdWallet
    })
    var address2 = wallet.addreETH(hdWallet)
    var trSign = wallet.expressMessage(hdWallet)
    var addressBtc = wallet.addreBTC(hdWallet)
    console.log('btcAddress:'+addressBtc);
    var signBTC = wallet.signatureBTCMess(hdWallet)
    console.log('signBTC:' + signBTC);
    return{
      btc_addr: addressBtc,//btc
      signature: trSign,//签名
      eth_addr: address2,
      message:'0xdeadbeaf',
      btc_message:'123456',
      btc_signature: signBTC
    }
  },
  inPassword(e){
    var id = e.target.id
    var value = e.detail.value
      if(id==1){
        this.setData({
          pswd:value
        })
      }else if(id ==2){
        this.setData({
          pswdAgin: value
        })
      }
  },
  restoreWallte(data,textData,pswd){
    var $this = this;
	//console.log(pswd)
    wallet.post('member/recover-wallet', data, function (res) {
      
        pswd = wallet.MD5jiami(pswd);
        //加密保存钱包
        //var mnemonic2 = textData.join(' ');
        var hdWallet = $this.data.Hdwallet
        var walletKey = JSON.stringify(hdWallet._hdkey.toJSON());
         var w = wallet.encryption(pswd, walletKey)
         wx.setStorageSync('walletKey', w)
         var address2 = wallet.addreETH(hdWallet)
         var addressBTC = wallet.addreBTC(hdWallet)
         var helpWord = wallet.encryption(pswd, textData.join(' '))
         wx.setStorageSync('addressETH', address2)
         wx.setStorageSync('addressBTC', addressBTC)
         wx.setStorageSync('pswd', pswd)//明文加密保存密码
         wx.setStorageSync('createUser', res.data)
         wx.setStorageSync('helpWord', helpWord)
         wx.reLaunch({
            url: '../../assetPage/assetPage'
          })
      },function(err){
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 500
        })
      }
    )
  },
  startPlay(e){
    if(this.data.checkedId){
      if(this.data.textData.length==12){//请求
      
        if (this.data.pswd == this.data.pswdAgin && this.data.pswd != "" && this.data.pswdAgin != ""){
          //console.log(this.data.pswd)
          var lang = wx.getStorageSync('lang')
          wx.showLoading({
            title: lang == "en" ? 'loading...' : '加载中...'
          })
          var data = this.helpWord(this.data.textData)
          this.restoreWallte(data,this.data.textData,this.data.pswd)
        } else if (this.data.pswd == '' || this.data.pswdAgin == ""){
          wx.showToast({
            title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].emptyPswd : wx.T.locales['en'].emptyPswd,
            icon: 'none',
            duration: 2000
          })
        }else {
          wx.showToast({
            title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].notConsistent : wx.T.locales['en'].notConsistent,
            icon: 'none',
            duration: 2000
          })
        }
      } else if (this.data.textData.length==0){
        wx.showToast({
          title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].mnemonicAndPassword : wx.T.locales['en'].mnemonicAndPassword,
          icon: 'none',
          duration: 2000
        })
      } else if (0 < this.data.textData.length<12) {
        //console.log(this.data.textData)
        wx.showToast({
          title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].mnemonicIsWrong : wx.T.locales['en'].mnemonicIsWrong,
          icon: 'none',
          duration: 2000
        })
      }
     
    }
  },
  radioChange(e){
      //console.log(e)
      this.setData({
        checkedId:!this.data.checkedId
      })
      //console.log(this.data.checkedId)
  },
  changContent(e){
    //console.log(e.target.id)
    this.setData({
      activeId:e.target.id,
      checkedId:false
    })
  },
  prativeKey(){
    wx.navigateTo({
      url: './Gosingle/Gosingle?id=9'
    })
  },
  keyStore(){

    wx.navigateTo({
      url: './Gosingle/Gosingle?id=8'
    })
    
  },
  servicePlay(){
    wx.navigateTo({
      url: './Gosingle/Gosingle?id=2'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      restoreLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']

    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].JLTabBarPropertyText : wx.T.locales['en'].JLTabBarPropertyText
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})